package com.peanut.controller;

import com.alibaba.fastjson.JSONObject;
import com.peanut.common.Result;
import com.peanut.domain.ov.TacCarBrandOV;
import com.peanut.dto.TabCarBrandDTO;
import com.peanut.server.TabBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("brand")
@Api
public class TabBrandController {
    private Logger logger = LoggerFactory.getLogger(TabBrandController.class);
    @Resource
    private TabBrandService tabBrandService;

    //查询全部信息
    @ApiOperation("查询全部信息")
    @GetMapping("all")
    public Result FindBrand() {
        try {
            List<TabCarBrandDTO> tabBrandBO = tabBrandService.FindBrand();
            logger.info("查询了全部", tabBrandBO);
            String s = JSONObject.toJSONString(tabBrandBO);
            List<TacCarBrandOV> tabBrandVOS = JSONObject.parseArray(s, TacCarBrandOV.class);
            return Result.OK().setData(tabBrandVOS);
        } catch (Exception e) {
            logger.error("查询出错", e);
            return Result.ERROR();
        }
    }
    @ApiOperation("模糊查询")
    @PostMapping("findBrandByName/{brandName}")
    @ApiImplicitParams({
            @ApiImplicitParam( name="brandName",value  ="品牌名称", paramType = "path",dataTypeClass = String.class),
    }
    )
    public Result findBrandByName(@PathVariable("brandName")String brandName)
    {
        try {
            List<TabCarBrandDTO> tabBrandBOS=tabBrandService.findBrandByName(brandName);
            logger.info("查询了",tabBrandBOS);
            String s = JSONObject.toJSONString(tabBrandBOS);
            List<TacCarBrandOV> tabBrandVOS=JSONObject.parseArray(s,TacCarBrandOV.class);
            return  Result.OK().setData(tabBrandVOS);
        }
        catch (Exception e)
        {
            logger.error("查询出错",e);
            return  Result.ERROR();
        }
    }
    //车辆类型
    @ApiOperation("增加品牌类型")
    @ApiImplicitParams({
            @ApiImplicitParam( name="TabBrand",value  ="品牌实体类", paramType = "path",dataTypeClass = String.class),
    }
    )
    @PostMapping("addBrand")
    public Result addBrand(@RequestBody TacCarBrandOV tacCarBrandOV)
    {
        try {
            TabCarBrandDTO tabBrandBO=new TabCarBrandDTO();
            BeanUtils.copyProperties(tabBrandBO,tacCarBrandOV);
            System.out.println(tabBrandBO);
            logger.info("添加了",tabBrandBO);
            tabBrandService.addBrand(tabBrandBO);
            return  Result.OK();
        }
        catch (Exception e)
        {
            logger.error("查询出错",e);
            return  Result.ERROR();
        }
    }
    //车辆类型
    @ApiOperation("批量删除品牌")
    @ApiImplicitParams({
            @ApiImplicitParam( name="id",value  ="多个id", paramType = "path",dataTypeClass = String.class),
    }
    )
    @PostMapping("deleteBathByIdsBrand/{ids}")
    public Result deleteBathByIdsBrand(@PathVariable("ids") String ids)
    {
        try {
            List<Long> list=new ArrayList<>();
            String[] split = ids.split(",");
            for (String s : split) {
                list.add(Long.parseLong(s));
            }
            logger.info("删除了",ids);
            tabBrandService.deleteBathByIdsBrand(list);
            return  Result.OK();
        }
        catch (Exception e)
        {
            logger.error("查询出错",e);
            return  Result.ERROR();
        }
    }
}

